package com.zx.mes.hyl.test.current;

import org.junit.Test;

import java.lang.Thread;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * CAS操作(乐观锁)
 *
 * CAS操作会通过对象实例中的偏移量直接进行赋值，因此，它不支持static字段（Unsafe.objectFieldOffset()不支持静态变量）。
 * @author hyl
 * @date 2018-2-23
 */
public class Current {

//    @Test
//    public void test(){
//        ExecutorService executorService=Executors.newFixedThreadPool(3,Executors.defaultThreadFactory());
//    }

    public final static AtomicReference<String> atomicStr = new AtomicReference<String>("神一般的存在");

    public final static AtomicStampedReference<Integer> atomicInt = new AtomicStampedReference<Integer>(19, 0);

    public final static AtomicInteger atomicInteger=new AtomicInteger(19);
    /**
     * AtomicReference 使用
     */
    @Test
    public void test(){
        for (int i=0;i<10;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep((int) Math.abs(Math.random()*100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (atomicStr.compareAndSet("神一般的存在", "风云")) {
                        System.out.println(Thread.currentThread().getId()+"值已改变");
                    }else {
                        System.out.println(Thread.currentThread().getId()+"失败");
                    }
                }
            }).start();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2(){
        final int tempstamp = atomicInt.getStamp();
        for (int i=0;i<3;i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        if (atomicInt.compareAndSet(atomicInt.getReference(), atomicInt.getReference() + 10,tempstamp,tempstamp+1)) {
                            System.out.println("只加一次值成功:"+atomicInt.getReference());
                            break;//跳出循环
                        }else {
                            System.out.println("加值失败");
                            break;
                        }
                    }

                }
            }).start();
        }
    }

    /**
     * AtomicInteger 注意可能出现的问题()
     */
    @Test
    public void test3(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i=0;i<3;i++) {
                    while (true) {
                        if (atomicInteger.compareAndSet(atomicInteger.get(), atomicInteger.get()+10)) {
                            System.out.println("值添加完成:"+atomicInteger.get());
                            break;
                        } else {
                            break;
                        }
                    }
                }
            }
        }).start();
    }
}
